using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace blimp_compass
{
    public partial class CompassControl : UserControl
    {

        private double _heading = 0;
        private double _goalHeading = 0;
        private double _needle_length = 60;
        private Pen needlePen = new Pen(Brushes.DarkGreen, 3.0F);
        private Font _compassFont = new Font("Arial", 12);

        public double Heading {

            set {
                _heading = value;
                this.Refresh();
                //this.tmrSmoothMove.Enabled = true;
            }

        }


        public CompassControl()
        {
            InitializeComponent();

            this.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer, true);

        }

        private void CompassControl_Load(object sender, EventArgs e)
        {
            _needle_length = this.Width * 0.40;

        }

        private void DrawControl(Graphics g)
        {
            g.Clear(this.BackColor);

            PointF center = new PointF((float)this.Width / 2.0F,
                                       (float)this.Height / 2.0F);

            double xFraction = Math.Cos(_heading - Math.PI / 2);
            double yFraction = Math.Sin(_heading - Math.PI / 2);

            g.FillEllipse(Brushes.White, new Rectangle(new Point(0,0), this.Size));

            SizeF stringSize = g.MeasureString("N", _compassFont);

            g.DrawString("N", _compassFont, Brushes.DarkRed,
                new PointF(center.X - stringSize.Width / 2, (float)(center.Y - this.Height / 2)));

            PointF needleTip = new PointF((float)(center.X + xFraction * _needle_length),
                                          (float)(center.Y + yFraction * _needle_length));

            g.DrawLine(needlePen, center, needleTip);

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (DesignMode) { base.OnPaint(e); return; }
                DrawControl(e.Graphics);            
            }

        public void GetInfo()
        {
            tmrSmoothMove.Enabled = false;
            MessageBox.Show(_heading.ToString() + " " + _goalHeading.ToString());
        }

        private void tmrSmoothMove_Tick(object sender, EventArgs e)
        {
            if (Math.Abs(_heading - _goalHeading) < 0.05)
            {
                tmrSmoothMove.Enabled = false;
                _heading = _goalHeading;
                this.Refresh();
            }
            else
            {
                if (goRight(_goalHeading, _heading))
                    _heading += 0.035;
                else
                    _heading -= 0.035;

                if (_heading < 0) _heading += 2*Math.PI;
                if (_heading > Math.PI*2) _heading -= 2 * Math.PI;


                this.Refresh();

            }
        }


        public bool goRight(double _goal, double _cur)
        {
            if (_goal > _cur)
            {
                if ((_goal - _cur) > (Math.PI * 2 - _goal + _cur))
                    return false;
                return true;
            }
            else
            {
                if ((_cur - _goal) > (Math.PI * 2 - _cur + _goal))
                    return true;
                return false;
            }
        }



        }


}
